package com.pshcp.manager.messagenotification;

import com.alibaba.fastjson.JSON;
import com.pshcp.domain.model.MessageNotification;
import com.pshcp.service.MessageNotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/12/6 17:14
 */
@Slf4j
@Component
public class MessageNotificationManager {

    @Autowired
    private MessageNotificationService messageNotificationService;

    @Async
    public void sendMessageNotification(MessageNotificationDTO messageNotificationDTO) {
        MessageNotification messageNotification = new MessageNotification();
        messageNotification.setTitle(messageNotificationDTO.getTitle());
        messageNotification.setDescription(messageNotificationDTO.getDescription());
        messageNotification.setContent(CollectionUtils.isEmpty(messageNotificationDTO.getContents()) ? null : JSON.toJSONString(messageNotificationDTO.getContents()));
        messageNotification.setMessageType(messageNotificationDTO.getMessageType());
        messageNotification.setUserType(messageNotificationDTO.getUserType());
        messageNotification.setUserId(messageNotificationDTO.getUserId());
        messageNotification.setDataId(messageNotificationDTO.getDataId());
        messageNotification.setReadFlag(0);
        messageNotificationService.insert(messageNotification);
        log.info("{} send msg", Thread.currentThread().getName());
    }
}
